<template>
    <div class="home-item" @click="onChat(data.id)">
        <div class="left">
            <div class="img">
                <img :src="data.avatar">
            </div>
        </div>
        <div class="center">
            <h5 class="title">{{data.nickname}}</h5>
            <p class="text">{{data.msg}}</p>
        </div>
        <div class="right">
            <span class="time">{{dateTime(data.update_time)}}</span>
            <span class="dot" v-if="data.unread != 0">{{data.unread}}</span>
        </div>
    </div>
</template>

<script>
export default {
    name: 'HomeItem',
    props: {
        data: {
            type: Object,
        }
    },
    data() {
        return {
        }
    },
    methods: {
        onChat(e) {
            this.$router.push(`/chat/${e}`)
        },
        dateTime(e) {
            let old = new Date(e)
            let now = new Date();
            let oY = old.getFullYear();
            let oM = old.getMonth()+1
            let oD = old.getDate()
            let oh = old.getHours()
            let om = old.getMinutes()
            if(om < 10){
                om = '0' + om
            }
            let nY = now.getFullYear()
            let nM = now.getMonth()+1
            let nD = now.getDate()
            if(oD === nD && oM === nM && oY === nY) {
                if(oh < 12) {
                    return '上午'+oh+':'+om
                }
                if(oh > 12) {
                    return '下午'+(oh-12)+':'+om
                }
                if(oh == 12){
                    return '中午'+oh+':'+om
                }
            }
            if(oD+1 === nD && oM === nM && oY === nY){
                return '昨天'
            }else{
                return oY+'-'+oM+'-'+oD
            }
        }
    },
}
</script>

<style lang="scss" scoped>
@import '../../assets/scss/theme.scss';
.home-item{
    width:100%;
    display: flex;
    align-items: center;
    padding:0.3125rem;
    position: relative;
    .left{
        margin-right:8px;
        .img{
            width:50px;
            height:50px;
            border-radius:100%;
            overflow: hidden;
            img{
                width:100%;
                height:100%;
                display: block;
                object-fit: cover;
            }
        }
    }
    .center{
        flex:1;
        overflow: hidden;
        .title{
            font-size:1rem;
            margin-bottom:6px;
            font-weight:normal;
            @include font_color("font_color1");
        }
        .text{
            font-size:0.9375rem;
            overflow:hidden;
            text-overflow:ellipsis;
            white-space:nowrap;
            width:90%;
            @include font_color("font_color9");
        }
    }
    .right{
        width:80px;
        position: absolute;
        right:0.3125rem;
        top:0.3125rem;
        bottom:0.3125rem;
        .time{
            position: absolute;
            top:0.125rem;
            right:0;
            font-size:0.875rem;
            @include font_color("font_color9");
        }
        .dot{
            position: absolute;
            bottom:0.3125rem;
            right:0;
            font-size:0.75rem;
            padding:0.125rem 0.3125rem;
            border-radius:10px;
            background:#F74C32;
            color:#fff;
        }
    }
}
</style>